/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ig.export.frame;

import ig.export.modelo.MPrincipalIGExport;
import ig.export.to.ConsultaTO;
import ig.export.to.ParametroTO;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author usuario
 */
public class MyTableModel extends AbstractTableModel {

    private boolean DEBUG = false;
    private boolean[] editables = null;
    private String[] estado = null;
    private String[] estadoAnterior = null;
    private Object[][] object;
    private String[] columnNames = null;
    private Object[][] data = null;
    private boolean delFisico = false;
    private Integer[] columnasDiferentes = new Integer[0];
    private MPrincipalIGExport modelo = null;

    public MyTableModel(Object[][] object, String[] columnNames, boolean[] editables) {
        this.editables = editables;
        this.columnNames = columnNames;
        if (object != null) {
            this.object = object;
        } else {
            this.object = new Object[0][columnNames.length];
        }
        setData(this.object);
    }

    public void setModelo(MPrincipalIGExport modelo) {
        this.modelo = modelo;
    }

    public boolean isDelFisico() {
        return delFisico;
    }

    public String[] getEstado() {
        return estado;
    }

    public void setData(Object[][] _data) {
        if (_data != null) {
            data = new Object[_data.length][columnNames.length];
            for (int j = 0; j < columnNames.length; j++) {
                for (int i = 0; i < _data.length; i++) {
                    this.data[i][j] = _data[i][j];
                }
            }
            this.fireTableDataChanged();
        } else {
            this.object = new Object[0][columnNames.length];
            this.data = new Object[0][columnNames.length];
        }
    }

    private void copyEstadosNuevos() {
        estadoAnterior = new String[estado.length];
        for (int i = 0; i < estado.length; i++) {
            estadoAnterior[i] = estado[i];
        }
    }

    private void copyEstadosViejos() {
        for (int i = 0; i < estadoAnterior.length; i++) {
            estado[i] = estadoAnterior[i];
        }
        copyEstadosNuevos();
    }

    //super.addTableModelListener()
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        try {
            return data[row][col];
        } catch (Exception e) {
            return "";
        }

    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        if (editables[col] == true) {
            return true;
        } else {
            return false;
        }
    }

    public Object[][] getData() {
        return this.data;
    }

    public Object[][] getObjet() {
        return this.object;
    }

    public void setValueAt(Object value, int row, int col) {
        if ( modelo != null && "0,3,4".contains(col+"")) {
            ConsultaTO consultaTO = modelo.getConsultaSeleccionada();
            if (consultaTO != null) {
                List<ParametroTO> parametroTOs = consultaTO.getTipo().equals(ConsultaTO.TIPO_JRXML) ? consultaTO.getTipoJrxmlTO().getParametroTOs() : consultaTO.getTipoSqlTO().getParametroTOs();
                for (ParametroTO parametroTO : parametroTOs) {
                    if (data[row][1].equals(parametroTO.getNombre())){
                        if (col==0){
                            parametroTO.setSolicitar(Boolean.valueOf(value.toString()));
                        }else if (col==3){
                            parametroTO.setValorPorDefecto(value.toString());
                        }else if (col==4){
                            parametroTO.setEtiqueta(value.toString());
                        }
                    }
                }
            }
            modelo.almacenarEnBaseDeDatos();
        }

        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")");
            System.out.println("New value of data:");
            printDebugData();
        }
        boolean existe = false;
        for (int i = 0; i < columnasDiferentes.length; i++) {
            if ((columnasDiferentes[i].intValue() - 1) == col) {
                for (int j = 0; j < data.length; j++) {
                    if (data[j][col].toString().trim().equals(value.toString().trim())) {
                        existe = true;
                        break;
                    }
                }
            }
            if (existe) {
                break;
            }
        }

        if (existe) {
            data[row][col] = "<Tipo Existente>";
        } else {
            data[row][col] = value;
        }
        setData(data);
        this.fireTableDataChanged();
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i = 0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j = 0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
    }

    public void moverArriba(int row) {
        Object[][] data_1 = new Object[this.data.length][this.getColumnCount()];
        for (int i = 0; i < data_1.length; i++) {
            for (int j = 0; j < this.getColumnCount(); j++) {
                data_1[i][j] = data[i][j];
            }
        }
        for (int i = 0; i < data_1.length; i++) {
            if (i == row) {
                for (int j = 0; j < this.getColumnCount(); j++) {
                    this.data[i - 1][j] = data_1[i][j];
                }
                for (int j = 0; j < this.getColumnCount(); j++) {
                    this.data[i][j] = data_1[i - 1][j];
                }
                break;
            }
        }
        this.fireTableDataChanged();
    }

    public void moverAbajo(int row) {
        Object[][] data_1 = new Object[this.data.length][this.getColumnCount()];
        for (int i = 0; i < data_1.length; i++) {
            for (int j = 0; j < this.getColumnCount(); j++) {
                data_1[i][j] = data[i][j];
            }
        }
        for (int i = 0; i < data_1.length; i++) {
            if (i == row) {
                for (int j = 0; j < this.getColumnCount(); j++) {
                    this.data[i + 1][j] = data_1[i][j];
                }
                for (int j = 0; j < this.getColumnCount(); j++) {
                    this.data[i][j] = data_1[i + 1][j];
                }
                break;
            }
        }
        this.fireTableDataChanged();
    }

    public Object nuevo() {
        ConsultaTO consultaTO = null;
        String nombre = JOptionPane.showInputDialog("Nombre para la consulta");
        if (nombre == null || nombre.trim().isEmpty()) {
            return consultaTO;
        }
        Object[][] data_1 = new Object[this.data.length + 1][this.getColumnCount()];
        for (int i = 0; i < data_1.length; i++) {
            for (int j = 0; j < this.getColumnCount(); j++) {
                try {
                    data_1[i][j] = data[i][j];
                } catch (Exception e) {
                    if (j == 0) {
                        data_1[i][j] = consultaTO = new ConsultaTO(nombre, ConsultaTO.TIPO_SQL);
                    } else {
                        data_1[i][j] = consultaTO = new ConsultaTO(nombre, ConsultaTO.TIPO_SQL);
                    }
                }

            }
        }
        this.data = data_1;
        this.fireTableDataChanged();
        return consultaTO;
    }

    public Object eliminar(int row) {
        Object objetoEliminado = null;
        if (row <= -1) {
            return null;
        }

        objetoEliminado = data[row][0];

        Object[][] data_1 = new Object[this.data.length - 1][this.getColumnCount()];
        int x = -1;
        for (int i = 0; i < data.length; i++) {
            if (i != row) {
                x++;
            }
            for (int j = 0; j < this.getColumnCount(); j++) {
                if (i != row) {
                    data_1[x][j] = data[i][j];
                }
            }
        }
        this.data = data_1;
        this.fireTableDataChanged();
        return objetoEliminado;
    }

    public Integer[] getColumnasDiferentes() {
        return columnasDiferentes;
    }

    public void setColumnasDiferentes(Integer[] columnasDiferentes) {
        this.columnasDiferentes = columnasDiferentes;
    }
}